﻿using System;
using System.Windows.Forms;
using SmartWallpaperChanger.Properties;

namespace SmartWallpaperChanger
{
    public partial class Config : Form
    {
        public event EventHandler SettingsSaved;

        public Config()
        {
            InitializeComponent();

            SetValues();
        }

        #region Helper

        private void SetValues()
        {
            // Dateisystem
            ObserverdFolderBox.Text = Settings.Default.ObservedFolder;
            
            // Dateitypen
            BitmapBox.Checked = true;
            JpegBox.Checked = Settings.Default.Extensions.Contains("jpg");
            GifBox.Checked = Settings.Default.Extensions.Contains("gif");
            PngBox.Checked = Settings.Default.Extensions.Contains("png");

            // Intervalle
            ChangeBox.Text = Settings.Default.ChangeInterval.ToString();
            VersionCheck.SelectedIndex = GetSelectedIndexFromVersionCheck();
            TimeoutBox.Text = Settings.Default.Timeout.ToString();

            // Neue Hintergründe
            OldBox.Text = Settings.Default.TimeUntilOld.Days.ToString();
            PrioBox.SelectedIndex = GetSelectedIndexFromEmphasis();

            // History
            HistoryBox.Text = Settings.Default.HistoryLength.ToString();

            // Autostart
            SetAutostartButton();
        }

        private void SaveValues()
        {
            try
            {
                if(ObserverdFolderBox.Text == string.Empty) throw new InvalidOperationException("Folder must be set.");

                Settings.Default.ObservedFolder = ObserverdFolderBox.Text;
                Settings.Default.Extensions = GetExtensionsString();
                Settings.Default.ChangeInterval = TimeSpan.Parse(ChangeBox.Text);
                Settings.Default.UpdateInterval = GetVersionCheckFromSelectedIndex();
                Settings.Default.Timeout = int.Parse(TimeoutBox.Text);
                Settings.Default.TimeUntilOld = TimeSpan.FromDays(double.Parse(OldBox.Text));
                Settings.Default.Emphasis = GetEmphasisFromSelectedIndex();
                Settings.Default.HistoryLength = int.Parse(HistoryBox.Text);
                Settings.Default.FirstRun = false;

                Settings.Default.Save();
                if(SettingsSaved != null) SettingsSaved(this, new EventArgs());
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sie haben ungültige Daten eingegeben: " + ex.Message);
            }
        }

        private string GetExtensionsString()
        {
            string extString = string.Empty;
            if (BitmapBox.Checked) extString += ",bmp";
            if (JpegBox.Checked) extString += ",jpg,jpeg";
            if (GifBox.Checked) extString += ",gif";
            if (PngBox.Checked) extString += ",png";

            return extString.Substring(1);
        }

        private int GetSelectedIndexFromEmphasis()
        {
            switch (Settings.Default.Emphasis)
            {
                case 1:
                    return 0;
                case 3:
                    return 1;
                case 5:
                    return 2;
                case 10:
                    return 3;
                default:
                    return 1;
            }
        }

        private int GetEmphasisFromSelectedIndex()
        {
            switch (PrioBox.SelectedIndex)
            {
                case 0:
                    return 1;
                case 1:
                    return 3;
                case 2:
                    return 5;
                case 3:
                    return 10;
                default:
                    return 1;
            }
        }

        private int GetSelectedIndexFromVersionCheck()
        {
            switch (Settings.Default.UpdateInterval)
            {
                case 1:
                    return 0;
                case 3:
                    return 1;
                case 7:
                    return 2;
                default:
                    return 0;
            }
        }

        private int GetVersionCheckFromSelectedIndex()
        {
            switch (VersionCheck.SelectedIndex)
            {
                case 0:
                    return 1;
                case 1:
                    return 3;
                case 2:
                    return 7;
                default:
                    return 1;
            }
        }

        private void SetAutostartButton()
        {
            if(Subro.AutoStart.Current.EnabledThroughStartupMenu)
            {
                AutostartLabel.Text = "Aktiviert";
                AutostartButton.Text = "Deaktivieren";
            }
            else
            {
                AutostartLabel.Text = "Deaktiviert";
                AutostartButton.Text = "Aktivieren";
            }
        }
        #endregion


        #region Eventhandler

        private void FolderBrowserButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
            
            DialogResult result = FolderBrowserDialog.ShowDialog();
            if(result == DialogResult.OK)
            {
                ObserverdFolderBox.Text = FolderBrowserDialog.SelectedPath;
            }
            
        }

        private void Abbrechen_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveValues();
        }

        private void AutostartButton_Click(object sender, EventArgs e)
        {
            Subro.AutoStart.Current.EnabledThroughStartupMenu = !Subro.AutoStart.Current.EnabledThroughStartupMenu;
            SetAutostartButton();
        }

        #endregion
    }
}
